﻿namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Xml;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuChanDoanHinhAnh : DevExpress.XtraEditors.XtraForm
    {
        string _MaChanDoanHinhAnh = string.Empty, _MaHoSoKhamBenh = string.Empty, _MaDichVu = string.Empty;
        Int32 _status;
        bool resultInsert = false;

        public event EventHandler _SavePhieuCanLamSang = null;

        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;
        PhieuChanDoanHinhAnhEntity phieuChanDoanHinhAnhEntity = null;

        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        PhieuChanDoanHinhAnhBusiness phieuChanDoanHinhAnhBusiness = new PhieuChanDoanHinhAnhBusiness();
        PhieuChanDoanHinhAnhChiTietBusiness phieuChanDoanHinhAnhChiTietBusiness = new PhieuChanDoanHinhAnhChiTietBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();

        public frmPhieuChanDoanHinhAnh(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;
            hoSoKhamBenhNgoaiTruEntity = obj;
            _MaDichVu = maDichVu;
            _MaChanDoanHinhAnh = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuCDHA);

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

        }
        public frmPhieuChanDoanHinhAnh(Int32 status, string maChanDoanHinhAnh)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);
                        
            _status = status;
            resultInsert = true;
            _MaChanDoanHinhAnh = maChanDoanHinhAnh;
            phieuChanDoanHinhAnhEntity = phieuChanDoanHinhAnhBusiness.GetPhieuChanDoanHinhAnh(_MaChanDoanHinhAnh);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuChanDoanHinhAnhEntity.MaHoSo);
            _MaDichVu = phieuChanDoanHinhAnhEntity.MaDichVu;

            ucThongTinHanhChinh.LoadThongTinHanhChinh(phieuChanDoanHinhAnhEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();

            ucChiDinhKhoaPhong.setMaKhoaKham(phieuChanDoanHinhAnhEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuChanDoanHinhAnhEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuChanDoanHinhAnhEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuChanDoanHinhAnhEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);
            LoadInfoPhieu(phieuChanDoanHinhAnhEntity);

        }
        void LoadInfoPhieu(PhieuChanDoanHinhAnhEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);

            txtKetQua.Html = obj.KetQua;
            txtKetLuan.Html = obj.KetLuan;
            txtAnh1.Text = obj.Anh1;
            txtAnh2.Text = obj.Anh2;
            txtAnh3.Text = obj.Anh3;
            txtAnh4.Text = obj.Anh4;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        private void btnAnh1_Click(object sender, EventArgs e)
        {
            txtAnh1.Text = FunctionList.FctOpenFileImage("Chọn hình 1");

        }

        private void btnAnh2_Click(object sender, EventArgs e)
        {
            txtAnh2.Text = FunctionList.FctOpenFileImage("Chọn hình 2");
        }

        private void btnAnh3_Click(object sender, EventArgs e)
        {
            txtAnh3.Text = FunctionList.FctOpenFileImage("Chọn hình 3");
        }

        private void btnAnh4_Click(object sender, EventArgs e)
        {
            txtAnh4.Text = FunctionList.FctOpenFileImage("Chọn hình 4");
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();

            if (resultInsert == false)
            {
                phieuChanDoanHinhAnhEntity = new PhieuChanDoanHinhAnhEntity()
                {
                    MaChanDoanHinhAnh = _MaChanDoanHinhAnh,
                    Stt = 0,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    KetQua = txtKetQua.Html.Trim(),
                    KetLuan = txtKetLuan.Html.Trim(),
                    MaBacSyKham = Authenticate.MaBacSy,
                    MaBacSyCls = string.Empty,
                    Anh1 = string.Empty,
                    Anh2 = string.Empty,
                    Anh3 = string.Empty,
                    Anh4 = string.Empty,
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaDichVu = _MaDichVu,
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                resultInsert = phieuChanDoanHinhAnhBusiness.InsertPhieuChanDoanHinhAnh(phieuChanDoanHinhAnhEntity);
            }
            else
            {
                phieuChanDoanHinhAnhEntity = phieuChanDoanHinhAnhBusiness.GetPhieuChanDoanHinhAnh(_MaChanDoanHinhAnh);
                phieuChanDoanHinhAnhEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuChanDoanHinhAnhEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuChanDoanHinhAnhEntity.KetQua = txtKetQua.Html.Trim();
                phieuChanDoanHinhAnhEntity.KetLuan = txtKetLuan.Html.Trim();
                phieuChanDoanHinhAnhEntity.Anh1 = string.Empty;
                phieuChanDoanHinhAnhEntity.Anh2 = string.Empty;
                phieuChanDoanHinhAnhEntity.Anh3 = string.Empty;
                phieuChanDoanHinhAnhEntity.Anh4 = string.Empty;
                phieuChanDoanHinhAnhEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuChanDoanHinhAnhEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuChanDoanHinhAnhEntity.MaDichVu = _MaDichVu;
                phieuChanDoanHinhAnhEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuChanDoanHinhAnhEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuChanDoanHinhAnhBusiness.EditPhieuChanDoanHinhAnh(phieuChanDoanHinhAnhEntity);

            }
            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(_MaChanDoanHinhAnh);
            //delete from table PhieuChanDoanHinhAnhChiTiet
            phieuChanDoanHinhAnhChiTietBusiness.DeletePhieuChanDoanHinhAnhChiTiet_ByMaPhieuChanDoanHinhAnh(_MaChanDoanHinhAnh);

            if (resultInsert == true)
            {
                //Insert into table ThuPhiDichVu, PhieuSieuAmChiTiet
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = _MaChanDoanHinhAnh,
                        MaHoSo = phieuChanDoanHinhAnhEntity.MaHoSo,
                        NgayDenKham = phieuChanDoanHinhAnhEntity.NgayVaoKham,
                        MaDichVu = _MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuChanDoanHinhAnhEntity.MaKhoaKham,
                        MaPhongKham = phieuChanDoanHinhAnhEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuChanDoanHinhAnhEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuChanDoanHinhAnhEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var PhieuChanDoanHAChiTiet = new PhieuChanDoanHinhAnhChiTietEntity()
                    {
                        MaChanDoanHinhAnh = _MaChanDoanHinhAnh,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };

                    phieuChanDoanHinhAnhChiTietBusiness.InsertPhieuChanDoanHinhAnhChiTiet(PhieuChanDoanHAChiTiet);
                }
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (_SavePhieuCanLamSang != null)
                _SavePhieuCanLamSang(sender, e);
        }

        private void frmPhieuChanDoanHinhAnh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh1.Text.Trim(), _MaChanDoanHinhAnh + "_Anh1");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh2.Text.Trim(), _MaChanDoanHinhAnh + "_Anh2");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh3.Text.Trim(), _MaChanDoanHinhAnh + "_Anh3");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh4.Text.Trim(), _MaChanDoanHinhAnh + "_Anh4");

            phieuChanDoanHinhAnhEntity = phieuChanDoanHinhAnhBusiness.GetPhieuChanDoanHinhAnh(_MaChanDoanHinhAnh);
            phieuChanDoanHinhAnhEntity.KetQua = txtKetQua.Html;
            phieuChanDoanHinhAnhEntity.KetLuan = txtKetLuan.Html;
            phieuChanDoanHinhAnhEntity.MaBacSyCls = Authenticate.MaBacSy;
            phieuChanDoanHinhAnhBusiness.EditPhieuChanDoanHinhAnh(phieuChanDoanHinhAnhEntity);
            MessageBox.Show("Cập nhật thành công bản ghi!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void frmPhieuChanDoanHinhAnh_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(_MaChanDoanHinhAnh, _MaDichVu);
        }

    }
}